//
//  WBStatusViewModel.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/10.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

class WBStatusViewModel: NSObject {
    
    var rowHeight: CGFloat = 0
    
    /// 配图的数组
    var picUrls: [WBStatusPicModel]?
    
    ///配图的size
    var pictureSize: CGSize = CGSize.zero
    
    /// 时间字符串
    var timeStr: String?
    
    /// 已处理过的vip的图标
    var vipImage: UIImage?
    
    /// 已处理过的皇冠等级的图标
    var levelImage: UIImage?
    
    /// 已处理好的来源的字符串
    var sourceText: String?
    
    var statusModel: WBStatusModel
    /// 指定构造器
    init(statusModel: WBStatusModel) {
        self.statusModel = statusModel
        super.init()
        
        //设置pic_urls
        setPicUrls()
        
        //处理vip的图标
        dealWithVipImage()
        
        //处理皇冠等级
        dealWithLevelImage()
        
        //处理来源字符串
        dealWithSource()
        
        //处理时间
        dealWithTime()
        
        //计算配图的高度
        caculatePictureSize()
        
        //计算行高
        caculateRowHeigh()
    }
    
    func caculateRowHeigh() {
        
    }
    
    func setPicUrls() {
        //原创有配图
        if let pic_urls = statusModel.pic_urls,  pic_urls.count > 0{
            picUrls = pic_urls
            return
        }
        
        //如果转发有配图
        if let pic_urls = statusModel.retweeted_status?.pic_urls,  pic_urls.count > 0{
            picUrls = pic_urls
        }
    }
    
    
    /// 计算配图的高度
    func caculatePictureSize() {
        //如果有配图，就来计算配图的高度
        if let pic_urls = picUrls {
            let rows = (pic_urls.count - 1)/3 + 1 //分页算法
            let imageHeight = homeVS.imageH
            
            let height = CGFloat(rows) * imageHeight + CGFloat(rows-1) * homeVS.gap
            let width = screenWidth - homeVS.gap*2
            
            let size = CGSize(width: width, height: height)
            pictureSize = size
        }
    }
    
    /// 处理时间字符串
    func dealWithTime() {
        if let create_at = statusModel.created_at {
            timeStr = Date.sinTimeToRequriedTimStr(sinaTime: create_at)
        }
    }
    
    /// 处理来源字符串
    func dealWithSource() {
        //判断是否有来源字符串
        if let source = statusModel.source, source.count > 0 {
            //<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">微博 weibo.com</a>
            let firstRange = source.range(of: "\">")
            let secondRange = source.range(of: "</a>")
            
            if let fr = firstRange, let sr = secondRange {
                let newRange = fr.upperBound..<sr.lowerBound
                sourceText = source.substring(with: newRange)
            }
        }
    }
    
    /// 处理等级
    func dealWithLevelImage() {
        if let level = statusModel.user?.mbrank {
            if level == 0 {
                levelImage = nil
                return
            }
            levelImage = UIImage(named: "common_icon_membership_level\(level)")
        }
    }
    
    /// 处理vip图标
    func dealWithVipImage() {
        if let verify_type = statusModel.user?.verified_type {
            switch verify_type {
            case 0:
                //达人
                vipImage = UIImage(named: "avatar_vip")
            case 2, 3, 5:
            //企业达人
                vipImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
            //草根达人
                vipImage = UIImage(named: "avatar_grassroot")
            default:
                vipImage = nil
            }
        }
    }
}
